<?php
/*********************************************************************************
* Filename: league_champ.php
* 
* This page is used to show the overall champion of the league.  This page
* is activated when all week games have been played.
*
*
* Author: Phillip Peterson
* Date Created: 11/30/2009
**********************************************************************************/
 
//Start the PHP session is it doesn't exist
if (!isset($_SESSION))
	session_start();
//Check if the team is logged in.  If not, redirect the user
//to the login page.
if (!isset($_SESSION['team_id'])){
	include_once "common.php";
	$path = $g_server . $g_login_page;

	header("Location: $path");
	die();
}
   
//The common.php file contains 
include_once "common.php";

//Store the team id which is stored in the session log
$team_id = $_SESSION['team_id'];

echo "$header_open";
echo "$css_league_home";
echo "$js_league_home";
echo "$header_close";

?>

<?php 
   
	//Query to get the current week for the league
	$query = "SELECT WEEK
			  FROM GAMES
			  WHERE ISNULL(TEAM_1_SCORE)";
	$results = do_query($query, $g_db_name, $g_username);
    
    //Check if all the league games are played.  If they aren't,
    //forward the user to the login page because they got to this
    //page on accident.
    if (mysql_num_rows($results) != 0){
        $path = $g_server . $g_login_page;
	    header("Location: $path");
	    die();
    }
    
    else {
?>

<body onload="postLoadLeague();">
	<div id="wrapper">
		<div id="inner">
			<?php
				echo "$g_nav_div";
			?>
			<div id="content">
				<div id="container-1" style="margin-bottom: 50px;">
					<ul class="ui-tabs-nav">
						<li class="ui-tabs-selected">
							<a href="#fragment-1">
								<span>Final League Standings</span>
							</a>
						</li>
					</ul>

					<div id="fragment-1" class="ui-tabs-panel">
						<table>
								<tr class="table_header">
									<td class="rank_cell rank_cell_header">Rank</td>
									<td class="team_cell team_cell_header">Team</td>
									<td class="point_cell point_cell_header">W-L-T</td>
									<td class="point_cell point_cell_header">Pct</td>
									<td class="point_cell point_cell_header">Pts</td>
									<td class="point_cell point_cell_header">Moves</td>
								</tr>
									<?php
                                   		//Select the win-tie-loss information for all of
										//the teams in the league.  This will be used to 
										//calculate the standings.
									    $query = "SELECT * 
                                                  FROM TEAMS 
                                                  WHERE LEAGUE_ID IN 
                                                      (SELECT LEAGUE_ID 
                                                      FROM TEAMS 
                                                      WHERE TEAM_ID=$team_id)
                                                      ORDER BY PCT DESC, POINTS DESC";
										$results = do_query($query, $g_db_name, $g_username);
    
                                        //Get information for each team in the league, so correct
                                        //rankings can be assigned
										while ($row = mysql_fetch_array($results)){
											$team_name[]    = $row['TEAM_NAME'];
											$team_wins[]    = $row['WIN'];
											$team_loss[]    = $row['LOSS'];
											$team_draw[]    = $row['DRAW'];
                                            $team_points[]  = $row['POINTS'];
                                            $team_pct[]     = $row['PCT'];
											$ids[]          = $row['TEAM_ID'];
										}

										$shade = true;
										$first = true;
										$champ = "";
										//Add a new row for every team in the league.  The teams are already
                                        //ordered by their rank from the query above.
										for($i = 0; $i < sizeof($team_name); $i++){
										    if ($first) {
										        $champ = $team_name[$i];
										        $first = false;
										    }
											//Add a new row to the table and fill in the
											//needed cells with each team's information
											$class = ($shade) ? "shade_odd" : "shade_even";
											$shade = !$shade;

											$record = $team_wins[$i] ."-". $team_loss[$i] ."-". $team_draw[$i];
                                            //Format the win percentage to have fixed number of decimal points
                                            $pct = number_format($team_pct[$i],3); 
                                            $rank = $i + 1;
											
											$query = "SELECT COUNT(*) AS NUM
													  FROM PEND_TRADE 
													  WHERE TEAM1 = {$ids[$i]} OR TEAM2 = {$ids[$i]}";
											$result = do_query($query, $g_db_name, $g_username);
											$row = mysql_fetch_assoc($result);
											$num = $row['NUM'];
        
                                            //Add the row to the table
											echo" <tr class=$class>";
											echo"   <td class='rank_cell'>$rank</td>";
											echo"   <td class='team_cell'>{$team_name[$i]}</td>";
											echo"   <td class='point_cell'>$record</td>";
											echo"   <td class='point_cell'>$pct</td>";
											echo"   <td class='point_cell'>{$team_points[$i]}</td>";
											echo"   <td class='point_cell'>$num</td>";
											echo" </tr>";
										}
									?>
							</table>
					</div>
				</div>
                <div id='champ'>
                    <?php echo "Congratulations <br>" . $champ . "!"; ?>
                </div>
			</div><!--Close content-->
			<script type='text/javascript'> var g_team_id  = <?php echo $team_id ?>;</script>

			<div id="sidebar">
                <div id="tropy_guy_final">
                </div>
			</div>	
			
			<div id="footer">
			  <?php echo "$footer"; ?>
			</div>
		</div>
	</div>
</body>

<?php } ?>
</html>
